package volatiledemo;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

public class VolatileDemoAtomicityDemo {
    public volatile static int inc;

    public synchronized void increse() {
        inc++;
    }

    public static void main(String[] args) throws InterruptedException {
        ExecutorService threadPool = Executors.newFixedThreadPool(500);
        VolatileDemoAtomicityDemo volaDemo = new VolatileDemoAtomicityDemo();
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 500; j++) {
                threadPool.submit(() -> {
                    volaDemo.increse();
                });
            }
        }
        Thread.sleep(1500);
        System.out.println(VolatileDemoAtomicityDemo.inc);
        threadPool.shutdown();
    }
}
